package com.crpc.core.channelHandler;

import com.crpc.core.CrpcBootStrap;
import com.crpc.core.transport.message.CrpcResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;

/**
 * @author CLOUD
 * @version 1.0
 * @description: 测试的类
 * @date 2023/9/10 14:17
 */
//入栈的handler
@Slf4j
public class MethodResponseHandler extends SimpleChannelInboundHandler<CrpcResponse> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, CrpcResponse crpcResponse) throws Exception {
        Object body = crpcResponse.getBody();
        body = body == null ? new Object() : body;
        //  做code处理
        //全局挂起的请求中寻找匹配的待处理的请求 completableFuture
        CompletableFuture<Object> completableFuture = CrpcBootStrap.PENDING_REQUEST.get(crpcResponse.getRequestId());
        completableFuture.complete(body);
        log.info("响应：{}，已经完获得服务提供发给我结果:===>{}", crpcResponse.getRequestId(), body);

    }
}
